﻿using System;
using System.Collections.Generic;
using SimpleServiceBus.Bus;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Endpoint;
using SimpleServiceBus.Endpoint.Metadata;

namespace SimpleServiceBus.Tests.Stubs
{
    public class ServiceLocatorOnlyMessageContext : IMessageContext
    {
        public ServiceLocatorOnlyMessageContext()
        {
            Services = new Dictionary<Type, object>();
        }

        public Dictionary<Type, object> Services { get; set; }

        #region IMessageContext Members

        public IMessagingEndpoint Endpoint
        {
            get { throw new NotImplementedException(); }
        }

        public IMessagePipeline MessagePipeline
        {
            get { throw new NotImplementedException(); }
        }

        public MessageEnvelope MessageEnvelope
        {
            get { throw new NotImplementedException(); }
        }

        public bool AbortMessageProcessing
        {
            get; set;
        }

        public IDictionary<string, object> MessageState
        {
            get { throw new NotImplementedException(); }
        }

        public IEndpointDescription EndpointMetadata
        {
            get { throw new System.NotImplementedException(); }
        }

        public T GetLocalService<T>()
        {
            if (Services.ContainsKey(typeof(T)))
                return (T) Services[typeof (T)];
            return default(T);
        }

        #endregion

        public ImplementationType RegisterService<ServiceType, ImplementationType>()
        {
            var impl = (ImplementationType) Activator.CreateInstance(typeof (ImplementationType));
            Services.Add(typeof (ServiceType), impl);
            return impl;
        }

        public void RegisterService<ServiceType>(object impl)
        {
            Services.Add(typeof (ServiceType), impl);
        }
    }
}